/* Copyright by GD GameDuell GmbH, All Rights Reserved
 * This document is strictly confidential and sole property of GD GameDuell GmbH, Berlin, Germany
 */

package de.gameduell.framework.game.cheat {
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import de.polygonal.ds.Array2;	

	/**
	 * Handles all game related cheat possibilities such as:
	 * <li>ClickBots (via click precisision)
	 * <li>Time cheats (e.g. !XSpeed or other tools) // TODO - not finished yet
	 * <li>Memory cheats (modify memory values)  // TODO - not finished yet
	 */
	public class CheatDetection {
	
		private var clickPoses:Array2;
		private var timeline:Stage;
		
		/**
		 * Class constructor
		 * 
		 * @param aStage	the main stage of an application
		 */
		public function CheatDetection(aStage:Stage){
			timeline = aStage;
			timeline.addEventListener(MouseEvent.MOUSE_DOWN, onMouseEvent);
			clickPoses = new Array2(timeline.width,timeline.height);
			clickPoses.fill(0);
		}

		/**
		 * Counts every mouseclick on the stage and put the current
		 * mouse coords into an array.
		 */
		private function onMouseEvent(event:MouseEvent):void {
			var clickNum:int = int(clickPoses.get(timeline.mouseX, timeline.mouseY));
			clickPoses.set(timeline.mouseX, timeline.mouseY, clickNum + 1);
		}
		
		/**
		 * Return number of coords where more than 3 clicks occured.
		 * 
		 * @return	number of multiclick-positions
		 */
		public function getBotIndex():int{
			var poses:Array = clickPoses.getArray();
			var count:int = 0;
			for(var i:* in poses){
				var value:int = int(poses[i]);
				if(value > 3){
					count += value;
				}
			}
			return count;
		}

		/**
		 * Returns a relationship between single and multiclicked coords.
		 * If a user doesn't hit a coordinate twice - 0 will be returned.
		 * 
		 * @return	relationship as float
		 */
		public function getClickPrecision():Number{
			var poses:Array    = clickPoses.getArray();
			var oneClick:int   = 1;
			var moreClicks:int = 0;
			for(var i:* in poses){
				var value:int = int(poses[i]);
				oneClick   = value == 1 ? oneClick + 1 : oneClick;
				moreClicks = value >= 3 ? moreClicks + 1 : moreClicks;
			}
			return  moreClicks / oneClick;
		}
	}
}
